package server.analytics;

/**
 * This class starts an analyticsserver.
 *
 * @version 1.0 vom 12.11.2012
 * @author Distributed Group 111 1026312, 1026309
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.NoSuchObjectException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

public class AnalyticsServerRun {

	public static void main(String[] args) {
		if (args.length != 1) {
			System.err
					.println("Usage: AnalyticsServerImpl <AnalyticsServerBinding>");
			System.exit(1);
		}

		BufferedReader inReader = new BufferedReader(new InputStreamReader(
				System.in));
		AnalyticsServer analyticsServer = new AnalyticsServerImpl();
		AnalyticsServer stub = null;
		Registry registry = null;
		String bindingName = args[0];

		// Read properties from registry.properties and get or create RMI-registry
		int registryPort = -1;
		String registryHost = "";
		java.io.InputStream is = ClassLoader
				.getSystemResourceAsStream("registry.properties");
		if (is != null) {
			java.util.Properties props = new java.util.Properties();
			try {
				props.load(is);
				registryPort = Integer.parseInt(props
						.getProperty("registry.port"));
				registryHost = props.getProperty("registry.host");
			} catch (IOException e) {
				System.err.println("ERROR: Reading from registry.properties:"
						+ e.getMessage());
			} catch (NumberFormatException e) {
				System.err
						.println("ERROR: Values in registry.properties file are no integers!");
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			System.err.println("Properties file not found!");
		}
		
		// try to create Registry
		try {
			registry = LocateRegistry.createRegistry(registryPort);
			System.out.println("RMI-registry successfully created.");
		} catch (RemoteException e) {} //if registry already exists, do nothing
				
		try {
			stub = (AnalyticsServer) UnicastRemoteObject.exportObject(
					analyticsServer, 0);
			registry = LocateRegistry.getRegistry(registryHost, registryPort);
			registry.bind(bindingName, stub);
			System.out
					.println("AnalyticsServer-object succussfully bound to registry.");
		} catch (RemoteException e) {
			System.err.println("ERROR: Could not bind Object to registry: "
					+ e.getMessage());
		} catch (AlreadyBoundException e) {
			System.err
					.println("ERROR: Object with specified name already bound: "
							+ e.getMessage());
		}
		
		try { // wait until the user enters "!exit"
			String userInput = "";
			while ((userInput = inReader.readLine()) != null) {
				if (userInput.equals("!exit")) {
					break;
				}
			}
		} catch (IOException e) {
			System.err.println("Error reading from stdin: " + e.getMessage());
			System.exit(1);
		}

		// free ressources, unbind and unexport object.
		try {
			inReader.close();
			((AnalyticsServerImpl) analyticsServer).closeAnalyticsServer();
			UnicastRemoteObject.unexportObject(analyticsServer, true);
			registry.unbind(bindingName);
			System.out.println("Analyticsserver successfully closed.");
		} catch (NoSuchObjectException e) {
			System.err.println("ERROR: Unexporting stub:" + e.getMessage());
			e.printStackTrace();
		} catch (NotBoundException e) {
			System.err
					.println("ERROR: NotBoundException: Registry closed!");
			System.out.println("Analyticsserver successfully closed.");
		} catch (AccessException e) {
			System.err.println("ERROR: AccesException: Registry closed!");
			System.out.println("Analyticsserver successfully closed.");
			// e.printStackTrace();
		} catch (RemoteException e) {
			System.err.println("ERROR: RemoteException: Registry closed!");
			System.out.println("Analyticsserver successfully closed.");
			// e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Error closing reader from stdin: "
					+ e.getMessage());
		}
	}
}